package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.palm.hubo2o.BaseFragment;
import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.OrderAdapter;
import com.palm.hubo2o.model.LazyResBody;
import com.palm.hubo2o.model.Order;
import com.palm.hubo2o.model.QueryOrdersParams;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.util.LazyHelper;
import com.palm.hubo2o.view.SimpleListDecoration;
import com.palm.palmutils.widget.EasyRecyclerView;
import com.palm.palmutils.widget.refreshlayout.RefreshLayout;

import butterknife.Bind;
import butterknife.BindColor;
import retrofit2.Call;

/**
 * Created by wongqi on 16/6/21.
 */
public class OrderChildFragment extends BaseFragment {
    @Bind(R.id.recycler_view)
    EasyRecyclerView mRecyclerView;
    @Bind(R.id.refresh_layout)
    RefreshLayout mRefreshLayout;
    @BindColor(R.color.dividerColorDark)
    int dividerColor;
    private String type;

    public static OrderChildFragment getInstance(@QueryOrdersParams.OrderType String type) {
        OrderChildFragment fragment = new OrderChildFragment();
        fragment.setType(type);
        return fragment;
    }

    @Override
    protected void onCreateView2(View root, Bundle savedInstanceState) {
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(new OrderAdapter());
        mRecyclerView.addItemDecoration(new SimpleListDecoration(dividerColor, 1));
        mRecyclerView.setDrawSelectorOnTop(true);
        mRecyclerView.setSelector(ContextCompat.getDrawable(getContext(), R.drawable.selector_foreground));
        mHelper.attach(mRefreshLayout, mRecyclerView, getActivity());
        mHelper.loadFirst();
//        ItemTouchHelper helper = new ItemTouchHelper(swipeCallback);
//        helper.attachToRecyclerView(mRecyclerView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mHelper.detach();
    }

    @Override
    protected int layoutId() {
        return R.layout.recyclerview_refreshable;
    }

    private void setType(String type) {
        this.type = type;
    }

    private LazyHelper<QueryOrdersParams, LazyResBody<Order>> mHelper = new LazyHelper<QueryOrdersParams, LazyResBody<Order>>() {
        @Override
        public void onLazyResponse(LazyResBody<Order> lazyResBody, boolean refresh) {
            OrderAdapter adapter = (OrderAdapter) mRecyclerView.getAdapter();
            adapter.setOrders(lazyResBody.getList(), refresh);
        }

        @Override
        public Call<Result<LazyResBody<Order>>> getLazyCall(Request<QueryOrdersParams> request) {
            return PalmRetrofit.getInstance().getApi().queryOrders(request);
        }

        @Override
        public Request<QueryOrdersParams> getLazyRequest() {
            QueryOrdersParams p = new QueryOrdersParams();
            p.setType(type);
            return new Request<>(p);
        }
    };
//
//    ItemTouchHelper.SimpleCallback swipeCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
//
//        @Override
//        public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//            OrderAdapter adapter = (OrderAdapter) mRecyclerView.getAdapter();
//            if (adapter.isPendingRemoval(viewHolder.getAdapterPosition())) {
//                return 0;
//            }
//            return super.getSwipeDirs(recyclerView, viewHolder);
//        }
//
//        @Override
//        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//            return false;
//        }
//
//        @Override
//        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
//            int swipedPosition = viewHolder.getAdapterPosition();
//            mAdapter.pendingRemoval(swipedPosition);
//            mAdapter.notifyItemChanged(swipedPosition);
//        }
//
//        @Override
//        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
//            super.onSelectedChanged(viewHolder, actionState);
//            final boolean swiping = actionState == ItemTouchHelper.ACTION_STATE_SWIPE;
//            mRefreshLayout.setEnabled(!swiping);
//        }
//    };
}
